package com.lihongman.web;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lihongman.dao.CompanyDao;
import com.lihongman.dao.DaoyouDao;
import com.lihongman.entity.Company;
import com.lihongman.entity.Daoyou;
import com.lihongman.entity.Message;
import com.lihongman.util.PKUtils;

public class SaleServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String task = request.getParameter("task");
		
		if("save".equals(task)){
			if(request.getMethod().equalsIgnoreCase("get")){
				toSave(request,response);
			}else{
				save(request,response);
			}
		}
	}

	private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Message message = new Message();
		message.setTime(request.getParameter("time"));
		message.setDanjia(Float.parseFloat(request.getParameter("money")));
		message.setRenshu(Integer.parseInt(request.getParameter("renshu")));
		message.setSource(request.getParameter("source"));
		
		String danwei = request.getParameter("danwei");
		String daoyou = request.getParameter("daoyou");
		
		CompanyDao com=new CompanyDao();
		Company company = com.findByName(danwei);
		
		if(company == null){
			company = new Company();
			company.setId(PKUtils.getPk());
			company.setCompany(danwei);
			com.save(company);
		}
		
		DaoyouDao dao = new DaoyouDao();
		Daoyou dyou = dao.findByName(daoyou);
		
		if(dyou == null){
			dyou = new Daoyou();
			dyou.setId(PKUtils.getPk());
			dyou.setName("name");
			dao.save(dyou);
		}
		
	}

	private void toSave(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		
	}
}
